import datetime
import requests
import json
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import *
from pyecharts.components import Table
from pyecharts.globals import ThemeType
from worldCountryName import itemCountry

def getChinaData():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&'
    request = requests.get(url=url).json()['data']
    data = json.loads(request)
    data = data['chinaTotal']
    item = {
        '国家中' : '中国',
        '国家英' : 'China',
        '新增确诊' : '',
        '总确诊' : data['confirm'],
        '疑似' : data['suspect'],
        '治愈' : data['heal'],
        '死亡' : data['dead'],
        '现有确诊' : data['nowConfirm'],

    }
    return item

def worldMap(url):
    requset = requests.get(url).json()['data']

    # data = json.loads(r)
    countryList = []
    for i in requset:

        item = {
            '国家中' : i['name'],  # 国家
            '国家英' : itemCountry[i['name']] if i['name'] in itemCountry else i['name'], # 如果没有国家英就显示原来的名字
            # '大洲':i['continent'], # 大洲
            # '日期':i['date'], # 日期
            # '是否更新':i['isUpdated'], # 是否更新
            '新增确诊' : i['confirmAdd'], # 新增确诊
            # '确认':i['confirmAddCut'], #确认
            '总确诊' : i['confirm'], # 总确诊
            '疑似' : i['suspect'], # 疑似
            '死亡' : i['dead'], # 死亡
            '治愈' : i['heal'], # 治愈
            '现有确诊' : i['confirm'], # 现有确诊
            # '证实':i['confirmCompare'], # 证实
            # '新增证实':i['nowConfirmCompare'], # 新增证实
            # '治愈证实':i['healCompare'], #治愈证实
            # '死亡证实':i['deadCompare'], # 死亡证实
        }
        countryList.append(item)

    countryList.append(getChinaData())
    dataFrame = pd.DataFrame(countryList)
    dataFrame.to_csv('./countryCSV/全球国家数据(%s).csv'%(datetime.date.today()))

    map = (
        Map(
            init_opts = opts.InitOpts(width = "800px", height = "600px", theme = ThemeType.DARK)
        )
        .add("国家", [list(z) for z in zip(list(dataFrame['国家英']), list(dataFrame['总确诊']))], "world")
        .set_series_opts(label_opts = opts.LabelOpts(is_show = False))
        .set_global_opts(
            title_opts = opts.TitleOpts(title = '世界疫情确诊地图'), # 标题
            visualmap_opts = opts.VisualMapOpts(
                max_ = 500000,
                is_piecewise = True,  # 颜色是否分段显示（False为渐变，True为分段）
                pieces = [
                    {'max': 0, 'min': 0, 'label': '0', 'color': 'white'},
                    {'max': 49, 'min': 1, 'label': '1-49', 'color': 'mistyrose'},
                    {'max': 99, 'min': 50, 'label': '50-99', 'color': 'pink'},
                    {'max': 999, 'min': 100, 'label': '100-999', 'color': 'salmon'},
                    {'max': 9999, 'min': 1000, 'label': '1000-9999', 'color': 'tomato'},
                    {'max': 99999, 'min': 10000, 'label': '10000-99999', 'color': 'orangered'},
                    {'max': 999999, 'min': 100000, 'label': '100000-999999', 'color': 'red'},
                    {'max': 9999999, 'min': 1000000, 'label': '1000000-9999999', 'color': 'brown'},
                    {'max': 99999999, 'min': 10000000, 'label': '≧10000000', 'color': 'darkred'}
                ]
            ),#根据颜色进行区分
            tooltip_opts = opts.TooltipOpts(
                is_show = True,  # 显示
                trigger_on = 'mousemove|click',
            )  # 鼠标点击或者移动到出现具体的数值)
        )
        # .render('./html文件/各国数据(%s).html'%(datetime.date.today()))
    )

    headers = dataFrame.columns.to_list()
    rows = list(dataFrame.values)
    # print(rows)
    table = (
        Table()
            .add(headers, rows)
            .set_global_opts(title_opts = opts.ComponentTitleOpts(title = "全球疫情数据汇总表"))
    )
    page = Page(layout = Page.SimplePageLayout)
    page.add(
        map,
        table,
    )
    page.render('./templates/全球国家数据(%s).html' % (datetime.date.today()))

if __name__ == '__main__':
    url = 'https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist'
    worldMap(url)